package com.example.work4.adapter.viewHolder


import android.content.Context
import android.content.Intent
import android.os.Handler
import android.view.View
import android.widget.Button
import android.widget.ImageView
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.example.work4.ImageActivity
import com.example.work4.MomentsActivity
import com.example.work4.R
import com.example.work4.http.MomentRetrofitImp
import com.example.work4.http.body.CommentSaveBody
import com.example.work4.po.Moments
import com.example.work4.view.CommentDialog


class MomentViewHolder (
    var dataBinding: ViewDataBinding,
    var avatar: ImageView = dataBinding.root.findViewById(R.id.avatar),
    var image: ImageView = dataBinding.root.findViewById(R.id.picture),
    var commentView: RecyclerView = dataBinding.root.findViewById(R.id.commentView),
    addComment: Button = dataBinding.root.findViewById(R.id.addComment),
    var momentId: Long = 0,var handler: Handler? = null
) : RecyclerView.ViewHolder(dataBinding.root),View.OnClickListener {
    var momentInfo:Moments? = null
init {
    addComment.setOnClickListener(this)
    image.setOnClickListener(this)
    avatar.setOnClickListener(this)
}
    var imageUri = "";
    var context:Context = super.itemView.context
    override fun onClick(v: View?) {
        when(v?.id) {
            R.id.addComment -> {
               CommentDialog.show(context,this)
            }
            R.id.sendComment -> {
                val api = MomentRetrofitImp()
                api.context = context
                if(handler!=null){
                    api.handler = handler;
                }
                val body = CommentSaveBody()
                body.momentId = this.momentId
                body.content = CommentDialog.input?.text.toString();
                api.addComment(body)
                CommentDialog.close()
            }
            R.id.avatar ->{
                val intent = Intent()
                intent.setClass(dataBinding.root.context,MomentsActivity::class.java)
                intent.putExtra("avatar",momentInfo!!.avatar)
                intent.putExtra("id",momentInfo!!.userId)
                intent.putExtra("nickname",momentInfo!!.username)
                dataBinding.root.context.startActivity(intent)
            }
            R.id.picture -> {
                val intent = Intent()
                intent.setClass(dataBinding.root.context,ImageActivity::class.java)
                intent.putExtra("imageUri",imageUri)
                dataBinding.root.context.startActivity(intent)
            }
        }

    }

}